package com.yijiaw.service;

import com.yijiaw.dao.PartDAO;
import com.yijiaw.dao.ParttypeDAO;

import com.yijiaw.domain.Part;
import com.yijiaw.domain.Parttype;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Part entities
 * 
 */

@Service("PartService")
@Transactional
public class PartServiceImpl implements PartService {

	/**
	 * DAO injected by Spring that manages Part entities
	 * 
	 */
	@Autowired
	private PartDAO partDAO;

	/**
	 * DAO injected by Spring that manages Parttype entities
	 * 
	 */
	@Autowired
	private ParttypeDAO parttypeDAO;

	/**
	 * Instantiates a new PartServiceImpl.
	 *
	 */
	public PartServiceImpl() {
	}

	/**
	 * Delete an existing Part entity
	 * 
	 */
	@Transactional
	public void deletePart(Part part) {
		partDAO.remove(part);
		partDAO.flush();
	}

	/**
	 * Load an existing Part entity
	 * 
	 */
	@Transactional
	public Set<Part> loadParts() {
		return partDAO.findAllParts();
	}

	/**
	 * Save an existing Parttype entity
	 * 
	 */
	@Transactional
	public Part savePartParttype(Integer id, Parttype related_parttype) {
		Part part = partDAO.findPartByPrimaryKey(id, -1, -1);
		Parttype existingparttype = parttypeDAO.findParttypeByPrimaryKey(related_parttype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingparttype != null) {
			existingparttype.setId(related_parttype.getId());
			existingparttype.setName(related_parttype.getName());
			related_parttype = existingparttype;
		} else {
			related_parttype = parttypeDAO.store(related_parttype);
			parttypeDAO.flush();
		}

		part.setProducttype(related_parttype);
		related_parttype.getParts().add(part);
		part = partDAO.store(part);
		partDAO.flush();

		related_parttype = parttypeDAO.store(related_parttype);
		parttypeDAO.flush();

		return part;
	}

	/**
	 * Return a count of all Part entity
	 * 
	 */
	@Transactional
	public Integer countParts() {
		return ((Long) partDAO.createQuerySingleResult("select count(o) from Part o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Part entity
	 * 
	 */
	@Transactional
	public void savePart(Part part) {
		Part existingPart = partDAO.findPartByPrimaryKey(part.getId());

		if (existingPart != null) {
			if (existingPart != part) {
				existingPart.setId(part.getId());
				existingPart.setName(part.getName());
				existingPart.setRemark(part.getRemark());
				existingPart.setImage(part.getImage());
			}
			part = partDAO.store(existingPart);
		} else {
			part = partDAO.store(part);
		}
		partDAO.flush();
	}

	/**
	 * Delete an existing Parttype entity
	 * 
	 */
	@Transactional
	public Part deletePartParttype(Integer part_id, Integer related_parttype_id) {
		Part part = partDAO.findPartByPrimaryKey(part_id, -1, -1);
		Parttype related_parttype = parttypeDAO.findParttypeByPrimaryKey(related_parttype_id, -1, -1);

		part.setProducttype(null);
		related_parttype.getParts().remove(part);
		part = partDAO.store(part);
		partDAO.flush();

		related_parttype = parttypeDAO.store(related_parttype);
		parttypeDAO.flush();

		parttypeDAO.remove(related_parttype);
		parttypeDAO.flush();

		return part;
	}

	/**
	 */
	@Transactional
	public Part findPartByPrimaryKey(Integer id) {
		return partDAO.findPartByPrimaryKey(id);
	}

	/**
	 * Return all Part entity
	 * 
	 */
	@Transactional
	public List<Part> findAllParts(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Part>(partDAO.findAllParts(startResult, maxRows));
	}
}
